package com.byzk.p2p.admin.base.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class SMSUtil {
	
	private static String timeOut = "connect timeout";

	private static String connect_err = "connect_error";
	
	/**
	 * 主要用于发送验证码
	 * 阿里大于：http://www.alidayu.com/
	 * 账号：xunboda2016
	 * 密码：xbd88238868
	 */
	
	/**
	 * 主要用于发送还款、提现等通知短信
	 * 杰驰短信平台：http://www.oa-sms.com/index.jsp
	 * 企业名：xbd2013
	 * 用户名：admin
     * 密码：789456
     * 
	 */
	
	/**
	 * 阿里大于--注册验证码
	 * @param phone
	 * @param code
	 * @return
	 */
	public static Boolean registerSMS(String phone,String code) {
		try {
			String url="http://gw.api.taobao.com/router/rest";
			String appkey="23311852";
			String secret="0880b5cd896d7dfd067a150fcc47ed36";
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend("");
			req.setSmsType("normal");
			req.setSmsFreeSignName("迅泊达");
			req.setSmsParamString("{\"code\":\""+code+"\",\"product\":\"迅泊达\"}");
			req.setRecNum(phone);
			req.setSmsTemplateCode("SMS_6085266");
			AlibabaAliqinFcSmsNumSendResponse rsp=client.execute(req);
			System.out.println(rsp.getResult().getSuccess());
			return rsp.getResult().getSuccess();
			
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void sendSMS(String phone,String content) {
		String url = "http://oa-sms.com/sendSms.action";
		String corpAccount = "xbd2013";
		String userAccount = "admin";
		String pwd = "789456";
		try {
			content = URLEncoder(content, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		pwd = MD5.GetMD5Code(pwd);
		StringBuffer param = new StringBuffer();
		param.append("corpAccount=").append(corpAccount);
		param.append("&userAccount=").append(userAccount);
		param.append("&pwd=").append(pwd);
		param.append("&mobile=").append(phone);
		param.append("&content=").append(content+"【迅泊达】");
		String ret = SendToHttp(url, param.toString());
	}
	
	/**
	 * 
	 * @param url
	 *            请求 URL
	 * @param parameter
	 *            参数列表
	 * @return
	 */
	public static String SendToHttp(String url, String parameter) {
		StringBuffer tmp = new StringBuffer();
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection) (new URL(url)).openConnection();
		} catch (Exception ex) {
			return connect_err;
		}
		if (connection != null) {
			try {
				connection.setRequestMethod("POST"); // 注意POST为大写的方式才可以，在实际中测试过遇到该问题
				connection.setRequestProperty("content-type",
						"application/x-www-form-urlencoded");
				System.setProperty("sun.net.client.defaultConnectTimeout",
						"30000");// 设置超时时间
				System
						.setProperty("sun.net.client.defaultReadTimeout",
								"30000");
				connection.setDoOutput(true);
				OutputStreamWriter writer = new OutputStreamWriter(connection
						.getOutputStream());
				writer.write(parameter);
				writer.flush();
				writer.close();
				BufferedReader read;
				read = new BufferedReader(new InputStreamReader(connection
						.getInputStream()));
				String str;
				while ((str = read.readLine()) != null) {
					tmp.append(str.trim());
				}
				read.close();// 关闭读取流
			} catch (Exception ex) {
				ex.printStackTrace();
				return timeOut;
			} finally {
				connection.disconnect();
			}
			return tmp.toString();
		} else {
			return connect_err;
		}
	}	
	/**
	 * 
	 * @param str
	 *            内容
	 * @param type
	 *            编码格式 utf-8 ISO8859_1，gbk,gb2312
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String URLEncoder(String str, String type)
			throws UnsupportedEncodingException {
		if (str == null) {
			return str;
		}
		str = java.net.URLEncoder.encode(str, type);

		return str;
	}
	
}	
	
